# coding=utf-8
from functools import cmp_to_key


class Student:
    def __init__(self, age, name):
        self.name = name
        self.age = age


def custom_sorted(stu1, stu2):
    if stu1.age < stu2.age:
        return -1
    if stu1.age > stu2.age:
        return 1
    return 0


stu1 = Student(18, "aaa")
stu2 = Student(28, "bbb")
stu3 = Student(21, "ccc")
student_list = sorted([stu1, stu2, stu3], key=lambda x: x.age)
for stu in student_list:
    print(f"{stu.name}-----{stu.age}")

student_list = sorted([stu1, stu2, stu3], key=cmp_to_key(custom_sorted))
for stu in student_list:
    print(f"cmp_to_key排序：{stu.name}-----{stu.age}")
